package com.hwl.lab.controller;

import com.hwl.lab.entity.Handle;
import com.hwl.lab.service.HandleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin //允许跨域请求
@RestController
public class HandleController {

    @Autowired
    private HandleService handleService;

    //查询所有（分页）
    @GetMapping("/findHandleAll/{page}/{size}")
    public Page<Handle> findHandleAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        return handleService.findAll(page,size);
    }
    //查询所有不分页
    @GetMapping("/findHandleAll")
    public List<Handle> findAll(){
        return handleService.findAll();
    }

    @GetMapping("/findByHandleidF/{handleid}")
    public List<Handle> findByUserid(@PathVariable("handleid") String handleid){
        if (handleService.findByHandleid(handleid) != null) {
            List<Handle> handles = new ArrayList<>();
            handles.add(handleService.findByHandleid(handleid));
            return handles;
        } else {
            return null;
        }
    }
    //添加，修改
    @PostMapping("/saveHandle")
    public void saveHandle(@RequestBody(required=false) Handle handle){
        handleService.saveHandle(handle);
    }

    //通过账号删除
    @PostMapping("/deleteHandle/{id}")
    public void deleteHandle(@PathVariable("id") Long id){
        handleService.deleteById(id);
    }
}
